﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.Xml.Linq;

using Centrica.Task.Model;
using Centrica.Task.App.Model;

namespace Centrica.Task.App.ViewModel
{
    public class TaskTimeStruct
    {
        private Centrica.Task.Model.Task _task;

        public string Description { get { return this._task.Description; } }
        public string Name { get { return this._task.Name; } }

        private DateTime Date1 { get; set; }
        private DateTime Date2 { get; set; }
        private DateTime Date3 { get; set; }
        private DateTime Date4 { get; set; }
        private DateTime Date5 { get; set; }

        public String DateVal1 { get { return GetStatusDisplay(GetDateStatus(this.Date1)); } }
        public String DateVal2 { get { return GetStatusDisplay(GetDateStatus(this.Date2)); } }
        public String DateVal3 { get { return GetStatusDisplay(GetDateStatus(this.Date3)); } }
        public String DateVal4 { get { return GetStatusDisplay(GetDateStatus(this.Date4)); } }
        public String DateVal5 { get { return GetStatusDisplay(GetDateStatus(this.Date5)); } }

        public System.Windows.Media.Color Color1
        {
            get { return TaskTimeStruct.GetStatusColor(GetDateStatus(this.Date1)); }
        }

        public System.Windows.Media.Color Color2
        {
            get { return TaskTimeStruct.GetStatusColor(GetDateStatus(this.Date2)); }
        }

        public System.Windows.Media.Color Color3
        {
            get { return TaskTimeStruct.GetStatusColor(GetDateStatus(this.Date3)); }
        }

        public System.Windows.Media.Color Color4
        {
            get { return TaskTimeStruct.GetStatusColor(GetDateStatus(this.Date4)); }
        }

        public System.Windows.Media.Color Color5
        {
            get { return TaskTimeStruct.GetStatusColor(GetDateStatus(this.Date5)); }
        }

        /// <summary>
        /// 
        /// </summary>
        private static System.Windows.Media.Color GetStatusColor(TaskStatus status)
        {
            return (
                status == TaskStatus.None ?
                    System.Windows.Media.Colors.White :
                        status == TaskStatus.Completed ?
                            System.Windows.Media.Colors.Red :
                                status == TaskStatus.Initiated ?
                                    System.Windows.Media.Colors.Pink :
                                        status == TaskStatus.InProgress ?
                                            System.Windows.Media.Colors.Beige :
                status == TaskStatus.Unauthorised ?
                    System.Windows.Media.Colors.Bisque :
                    System.Windows.Media.Colors.White);
        }

        private static String GetStatusDisplay(TaskStatus status)
        {
            return (
                status == TaskStatus.None ?
                    "N" : status == TaskStatus.Completed ?
                            "C" : status == TaskStatus.Initiated ?
                                    "I" : status == TaskStatus.InProgress ?
                                            "P" : status == TaskStatus.Unauthorised ?
                                                "U" : String.Empty);
        }

        private TaskStatus GetDateStatus(DateTime dt)
        {
            return (
                dt < this._task.StartDate ||
                dt > this._task.FinishDate.AddDays(1) ?
            Centrica.Task.Model.TaskStatus.None : this._task.Status
            );
        }

        public TaskTimeStruct(Centrica.Task.Model.Task task)
        {
            this._task = task;
        }

        public void Initialise(DateTime dtFrom)
        {
            this.Date1 = dtFrom;
            this.Date2 = dtFrom.AddDays(1);
            this.Date3 = dtFrom.AddDays(2);
            this.Date4 = dtFrom.AddDays(3);
            this.Date5 = dtFrom.AddDays(4);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    public class TaskTimeViewModel
    {
        private List<TaskTimeStruct> _taskTimeStructList;

        protected TaskTimeViewModel()
        {
            this._taskTimeStructList = new List<TaskTimeStruct>();
        }

        public static TaskTimeViewModel CreateInstance()
        {
            return new TaskTimeViewModel();
        }

        public List<TaskTimeStruct> TaskTimeData(DateTime dtFrom)
        {
            /// List<Centrica.Task.Model.Task> taskList =
            List < Centrica.Task.Model.Task > taskList = new List<Centrica.Task.Model.Task>(
                TaskModelFactoryProvider.StaticInstance.Provider
                .GetAllTasks()
                .Select(t => t));

            foreach (Centrica.Task.Model.Task task in taskList)
            {
                TaskTimeStruct timeStruct = new TaskTimeStruct(task);
                timeStruct.Initialise(dtFrom);
                this._taskTimeStructList.Add(timeStruct);
            }
            return this._taskTimeStructList;
        }
    }
}
